package com.talkweb.securitycamera.data.hadoopDB;

import com.talkweb.securitycamera.common.constans.FileType;
import com.talkweb.securitycamera.common.interfaces.dataprocess.FileCallback;
import com.talkweb.securitycamera.common.interfaces.dataprocess.IFileGet;
import com.talkweb.securitycamera.common.interfaces.dataprocess.IFileProcessor;
import com.talkweb.securitycamera.data.DbConstant;
import com.talkweb.securitycamera.data.bean.MediaEntity;
import com.talkweb.securitycamera.data.hadoopFile.BaseMediaProcessor;
import com.talkweb.securitycamera.data.hadoopFile.HadoopFileFactory;
import ohos.app.Context;
import ohos.data.distributed.common.Query;
import ohos.media.photokit.metadata.AVMetadataHelper;

import java.io.File;
import java.util.List;

import com.talkweb.securitycamera.data.DbConstant.ColumeName;

/**
 * @version 1.0.1
 * @description:分布式数据库内容提供
 * @program: security-camera
 * @Author xiaozhijun
 * @Date 2021/1/28 16:08
 */
public class HadoopDbProvider implements IFileProcessor<MediaEntity, String>, IFileGet {

    /**
     * 该数据库的唯一标识
     */
    private static final String storeID = "media_store";

    private Context ctx;

    /**
     * UI刷新回调接口
     */
    private FileCallback fileCallback;

    public HadoopDbProvider(Context context, FileCallback callback) {
        this.ctx = context.getApplicationContext();
        this.fileCallback = callback;
    }

    @Override
    public List<MediaEntity> getFileList(FileType type) {
        HadoopHelper helper = HadoopDbFactory.getInstance().openHadoop(ctx, storeID, fileCallback);
        Query query = Query.select().equalTo(ColumeName.COLUME_FILETYPE, type.getValue()).and().orderByDesc(ColumeName.COLUME_CREATETIME);
        return helper.query(query);
    }

    @Override
    public List<MediaEntity> getFileListByTime(long startTime, long endTime, FileType type) {
        HadoopHelper helper = HadoopDbFactory.getInstance().openHadoop(ctx, storeID, fileCallback);
        Query query = Query.select().greaterThanOrEqualTo(ColumeName.COLUME_CREATETIME, startTime)
                .and().equalTo(ColumeName.COLUME_FILETYPE, type.getValue())
                .and().lessThan(ColumeName.COLUME_CREATETIME, endTime)
                .and().orderByAsc(ColumeName.COLUME_CREATETIME);
        return helper.query(query);
    }

    @Override
    public List<MediaEntity> getFileListByKeyword(String keyword, FileType type) {
        HadoopHelper helper = HadoopDbFactory.getInstance().openHadoop(ctx, storeID, fileCallback);
        Query query = Query.select().like(ColumeName.COLUME_FILENAME, keyword)
                .and().equalTo(ColumeName.COLUME_FILETYPE, type.getValue());
        return helper.query(query);
    }

    @Override
    public void addFile(String source, FileType type) {
        MediaEntity entity = new MediaEntity();
        File sourceFile = new File(source);
        entity.setFileName(sourceFile.getName());
        entity.setCreateTime(sourceFile.lastModified());
        entity.setFilePath(source);
        entity.setType(type);

        if (type == FileType.FILE_TYPE_VIDEO) {
            AVMetadataHelper avMetadataHelper = new AVMetadataHelper();
            avMetadataHelper.setSource(sourceFile.getAbsolutePath());
            entity.setCreateDuration(avMetadataHelper.resolveMetadata(AVMetadataHelper.AV_KEY_DURATION));
        }
        HadoopHelper helper = HadoopDbFactory.getInstance().openHadoop(ctx, storeID, fileCallback);
        helper.insert(entity);
    }

    @Override
    public File createSourceFile(FileType fileType) {
        File result = null;
        BaseMediaProcessor processor = HadoopFileFactory.getInstance().getMediaProcessor(fileType, ctx);
        if (processor != null) {
            result = processor.createSourceFile(fileType);
            System.out.println("文件路径："+result.getAbsolutePath());
        }
        return result;
    }
}
